package com.yidu.logisticssystem.service.impl;

import com.yidu.logisticssystem.dao.LogisticsStateDao;
import com.yidu.logisticssystem.entity.LogisticsSignbill;
import com.yidu.logisticssystem.dao.LogisticsSignbillDao;
import com.yidu.logisticssystem.entity.LogisticsStaff;
import com.yidu.logisticssystem.entity.LogisticsState;
import com.yidu.logisticssystem.entity.LogisticsUser;
import com.yidu.logisticssystem.service.LogisticsSignbillService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 用户签单表(LogisticsSignbill)表服务实现类
 *
 * @author yuxigan
 * @since 2021-03-08 16:46:54
 */
@Service("logisticsSignbillService")
public class LogisticsSignbillServiceImpl implements LogisticsSignbillService {
    /**
     * 用户签单
     */
    @Resource
    private LogisticsSignbillDao logisticsSignbillDao;
    /**
     * 员工
     */
    @Resource
    private LogisticsStateDao stateDao;

    /**
     * 查询已接的订单
     * @param logisticsSignbill 实例对象
     * @return
     */
    @Override
    public List<LogisticsSignbill> queryway(LogisticsSignbill logisticsSignbill) {
        return logisticsSignbillDao.queryway(logisticsSignbill);
    }

    /**
     * 查询未接的订单
     * @param logisticsSignbill 实例对象
     * @return
     */
    @Override
    public List<LogisticsSignbill> query(LogisticsSignbill logisticsSignbill) {
        return logisticsSignbillDao.query(logisticsSignbill);
    }

    /**
     * 条件查询所有
     * @param logisticsSignbill 实例对象
     * @return
     */
    @Override
    public List<LogisticsSignbill> queryAll(LogisticsSignbill logisticsSignbill,HttpServletRequest request) {
        //从Session取出用户对象
        LogisticsUser user = (LogisticsUser)request.getSession().getAttribute("user");
        if(user.getUserId()!=null) {
            logisticsSignbill.setUserId(user.getUserId());
        }
        return logisticsSignbillDao.queryAll(logisticsSignbill);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param signbillId 主键
     * @return 实例对象
     */
    @Override
    public LogisticsSignbill queryById(Integer signbillId) {
        return this.logisticsSignbillDao.queryById(signbillId);
    }

    @Override
    public LogisticsSignbill queryWaynum(String wayNum) {
        return logisticsSignbillDao.queryWaynum(wayNum);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<LogisticsSignbill> queryAllByLimit(int offset, int limit) {
        return this.logisticsSignbillDao.queryAllByLimit(offset, limit);
    }
    /**
     * 新增数据
     *
     * @param logisticsSignbill 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional //事务注解
    public int insert(LogisticsSignbill logisticsSignbill, HttpServletRequest request) {
        //随机生成一个订单编号
        UUID o=UUID.randomUUID();
        //截取
        String s=o.toString().substring(0,14);
        Date date = new Date();
        SimpleDateFormat si=new SimpleDateFormat("yyyy-MM-dd");
        String dates=si.format(date);
        System.out.println("生成订单编号为"+s+dates);
        //加入订单编号
        logisticsSignbill.setWayNum(s+dates);
        //下单日期  加入当前日期
        logisticsSignbill.setSbDate(java.sql.Date.valueOf(dates));
        //从Session取出用户id
        LogisticsUser user = (LogisticsUser)request.getSession().getAttribute("user");
        logisticsSignbill.setUserId(user.getUserId());

        //-----------生成货运状态------------
        LogisticsState state=new LogisticsState();
        //设置详细id
        state.setDetailtypeId(1);
        //设置货运单编号
        state.setWayNum(s+dates);
        //设置发生时间
        state.setStateTime(java.sql.Date.valueOf(dates));
        //设置完成进度
        state.setStaTai(0);
        stateDao.insert(state);
        return logisticsSignbillDao.insert(logisticsSignbill);
    }

    /**
     * 修改数据
     *
     * @param logisticsSignbill 实例对象
     * @return 实例对象
     */
    @Override
    public int update(LogisticsSignbill logisticsSignbill) {
        return logisticsSignbillDao.update(logisticsSignbill);
    }

    /**
     * 通过主键删除数据
     *
     * @param signbillId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer signbillId) {
        return this.logisticsSignbillDao.deleteById(signbillId) > 0;
    }
}
